<?php
$serverName = "localhost"; //serverName\instanceName
/*$options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
); */
//$connectionInfo = array( "Database"=>"caraburo_mathvideos", "UID"=>"caraburo_videos", "PWD"=>"336699");

try{				    	
		//$conn = new PDO("mysql:host=localhost;dbname=caraburo_mathvideos", "caraburo_videos", "336699",$options);
		$conn=new PDO("pgsql:dbname=mathvideos;host=127.0.0.1", "nsfevents","nsf2006");
		$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  // put error to exception
		$conn2=new PDO("pgsql:dbname=nsfevents;host=127.0.0.1", "nsfevents","nsf2006");
		$conn2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  // put error to exception
	} 
catch (PDOException $e) 
	{
		$msg=$e->getMessage();
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}
function db_select_query($conn,$tsql,$params=array())
{
	if(isset($params) && $params !='')
	{
		try
		{
			$result = $conn->prepare("$tsql");
			$result->execute($params);
		}
		catch (PDOException $e) 
		{
			 $msg = $e->getMessage();
				print "Error!: " . $e->getMessage() . "<br/>";
				die();
		}
	}
	else
	{
		try
		{
			$result = $conn->query("$tsql");
		} 
		catch (PDOException $e) 
		{
			 $msg = $e->getMessage();
				print "Error!: " . $e->getMessage() . "<br/>";
				die();
		}
	}

	return $result;
}
function db_change_query($conn,$tsql,$params=array())
{
	global $to;
	global $user_id;
	global $institute_id;
  try{
		//echo $tsql;
   $sth = $conn->prepare ($tsql);
  
   # pass values directly to execute() as an array
   $sth->execute ($params);
	}
	catch (PDOException $e) 
	{
		 $msg = $e->getMessage();
	//	  print "Error!: " . $e->getMessage() . "<br/>";
		//	echo "message=$message<br>";
		//	die();
			$sqltext="INSERT INTO logs(error,added_date,user_id, institute_id) values( ?, ?, ?,?)";
			$params = array();
			array_push($params,sql_escape($msg));
			array_push($params,date("Y-m-d H:i:s"));
				array_push($params,sql_escape($user_id));
			array_push($params,sql_escape($institute_id));
			db_change_query($conn,$sqltext,$params);
			//send email to admin
			$headers = "MIME-Version: 1.0\n" ;
			$headers .= 'MIME-Version: 1.0' . "\n";
			$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
			$subject ='API error';
			mail($to, $subject, $msg,$headers);
			 //let user know the error
			header('HTTP/1.0 500 ');
	    // print "{\"error\": \"Server Error, and the admin is notified, sorry for the inconvinience\"}";
			print "Server Error. Admin is notified, sorry for the inconvinience!";
      die();
			/*
				
		 //let user know the error
		 header('HTTP/1.0 500 ');
		 echo "{\"error\": \"Server Error: the admin is notified, sorry for the inconvinience\"}";
		 exit;*/
	}
}

function db_num_rows($result)
{
	
	return  $result->rowCount();
}
function db_fetch_array($result)
{
	return $result->fetch(PDO::FETCH_BOTH);
}
function db_fetch_object($result)
{
	return $result->fetch(PDO::FETCH_OBJ);
}
function db_close( $conn)
{
	unset($conn);
}
function sql_escape($str,$html=0)
{
  global $purifier;
 
  $str=stripslashes(trim($str));
  
  if($html==1)
  {
    //purify html
	 $str=$purifier->purify($str);
  }
  else
  {
    $str=htmlspecialchars(strip_tags($str));
  }
  return $str;
}
function html_escape($str)
{
  return strip_tags($str);
}
function displayTree($array) {
			 $newline = "<br>";
			 foreach($array as $key => $value) {    //cycle through each item in the array as key => value pairs
					 if (is_array($value) || is_object($value)) {        //if the VALUE is an array, then
							//call it out as such, surround with brackets, and recursively call displayTree.
							 $value = "{<ul>" . displayTree($value) . "</ul>&nbsp;&nbsp;&nbsp;}";
					 }
					//if value isn't an array, it must be a string. output its' key and value.
					$output .= "&nbsp;&nbsp;&nbsp;\"$key\": " . $value . $newline;
			 }
			 return $output;
	}
function paginate($total,$limit_row,$cur_page,$color="#ffffff")
{
	global $pagename;
	$totalrows = $total;
	$limit = $limit_row;
	$page = $cur_page;
	$numofpages = ceil($totalrows/ $limit);
	$total_showpage = ceil($numofpages/5);

	if(empty($page))
	{
       $page = 1;
	     $showpage = 1;
   }
   // $limitvalue = $limit*($page-1);
   if($page >= 1)
	 {
      $showpage = (int)(($page-1)/5) +1;//every page navigate has 5 pages
		  $pageprev = ($showpage-1)*5;
      if($pageprev != 0)
		  {
        	echo "<a href=\"{$pagename}page=$pageprev\" style='color:$color;'> Prev</a>&nbsp;";
		  }
   }
 	 if($showpage == $total_showpage)
	 {
		  $start = 1+($showpage-1)*5;
		  for($i = $start; $i <= $numofpages; $i++)
		  {
   	     	if($i == $page)
			    {
     	       echo "<b style='color:#ff8c00;text-decoration:underline;'>".$i."</b>&nbsp;";
     	   	}
			    else
			    {
      	      echo "<a href=\"{$pagename}page=$i\" style='color:$color;'>$i</a>&nbsp;";
       	 	}
  	  }
	 }
	 else
	 {
		  $start = 1+($showpage-1)*5;
		  $end = 5 +($showpage-1)*5;
  	  for($i = $start; $i <= $end; $i++)
		  {
       	 	if($i == $page)
			    {
     	       echo "<b style='color:#ff8c00;text-decoration:underline;'>".$i."</b>&nbsp;";
     	   	}
			    else
			    {
           	 echo "<a href=\"{$pagename}page=$i\" style='color:$color;'> $i</a>&nbsp;";
      	  }
    	}
	 }

	 if($showpage < $total_showpage)
	 {
		   $pagenext = $showpage*5+1;
       echo "<a href=\"{$pagename}page=$pagenext\" style='color:$color;'> Next&nbsp;</a>";
   }
   // return $limitvalue;
 }
?>